<?php

// 公共助手函数

use think\exception\HttpResponseException;
use think\Response;

if (!function_exists('__')) {

    /**
     * 获取语言变量值
     * @param string $name 语言变量名
     * @param string | array  $vars 动态变量值
     * @param string $lang 语言
     * @return mixed
     */
    function __($name, $vars = [], $lang = '')
    {
        if (is_numeric($name) || !$name) {
            return $name;
        }
        if (!is_array($vars)) {
            $vars = func_get_args();
            array_shift($vars);
            $lang = '';
        }
        return \think\Lang::get($name, $vars, $lang);
    }
}

if (!function_exists('format_bytes')) {

    /**
     * 将字节转换为可读文本
     * @param int    $size      大小
     * @param string $delimiter 分隔符
     * @param int    $precision 小数位数
     * @return string
     */
    function format_bytes($size, $delimiter = '', $precision = 2)
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        for ($i = 0; $size >= 1024 && $i < 5; $i++) {
            $size /= 1024;
        }
        return round($size, $precision) . $delimiter . $units[$i];
    }
}

if (!function_exists('datetime')) {

    /**
     * 将时间戳转换为日期时间
     * @param int    $time   时间戳
     * @param string $format 日期时间格式
     * @return string
     */
    function datetime($time, $format = 'Y-m-d H:i:s')
    {
        $time = is_numeric($time) ? $time : strtotime($time);
        return date($format, $time);
    }
}

if (!function_exists('human_date')) {

    /**
     * 获取语义化时间
     * @param int $time  时间
     * @param int $local 本地时间
     * @return string
     */
    function human_date($time, $local = null)
    {
        return \fast\Date::human($time, $local);
    }
}

if (!function_exists('cdnurl')) {

    /**
     * 获取上传资源的CDN的地址
     * @param string  $url    资源相对地址
     * @param boolean $domain 是否显示域名 或者直接传入域名
     * @return string
     */
    function cdnurl($url, $domain = false)
    {
        $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
        $cdnurl = \think\Config::get('upload.cdnurl');
        if (is_bool($domain) || stripos($cdnurl, '/') === 0) {
            $url = preg_match($regex, $url) || ($cdnurl && stripos($url, $cdnurl) === 0) ? $url : $cdnurl . $url;
        }
        if ($domain && !preg_match($regex, $url)) {
            $domain = is_bool($domain) ? request()->domain() : $domain;
            $url = $domain . $url;
        }
        return $url;
    }
}


if (!function_exists('is_really_writable')) {

    /**
     * 判断文件或文件夹是否可写
     * @param string $file 文件或目录
     * @return    bool
     */
    function is_really_writable($file)
    {
        if (DIRECTORY_SEPARATOR === '/') {
            return is_writable($file);
        }
        if (is_dir($file)) {
            $file = rtrim($file, '/') . '/' . md5(mt_rand());
            if (($fp = @fopen($file, 'ab')) === false) {
                return false;
            }
            fclose($fp);
            @chmod($file, 0777);
            @unlink($file);
            return true;
        } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
            return false;
        }
        fclose($fp);
        return true;
    }
}

if (!function_exists('rmdirs')) {

    /**
     * 删除文件夹
     * @param string $dirname  目录
     * @param bool   $withself 是否删除自身
     * @return boolean
     */
    function rmdirs($dirname, $withself = true)
    {
        if (!is_dir($dirname)) {
            return false;
        }
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );

        foreach ($files as $fileinfo) {
            $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
            $todo($fileinfo->getRealPath());
        }
        if ($withself) {
            @rmdir($dirname);
        }
        return true;
    }
}

if (!function_exists('copydirs')) {

    /**
     * 复制文件夹
     * @param string $source 源文件夹
     * @param string $dest   目标文件夹
     */
    function copydirs($source, $dest)
    {
        if (!is_dir($dest)) {
            mkdir($dest, 0755, true);
        }
        foreach (
            $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
                RecursiveIteratorIterator::SELF_FIRST
            ) as $item
        ) {
            if ($item->isDir()) {
                $sontDir = $dest . DS . $iterator->getSubPathName();
                if (!is_dir($sontDir)) {
                    mkdir($sontDir, 0755, true);
                }
            } else {
                copy($item, $dest . DS . $iterator->getSubPathName());
            }
        }
    }
}

if (!function_exists('mb_ucfirst')) {
    function mb_ucfirst($string)
    {
        return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
    }
}

if (!function_exists('addtion')) {

    /**
     * 附加关联字段数据
     * @param array $items  数据列表
     * @param mixed $fields 渲染的来源字段
     * @return array
     */
    function addtion($items, $fields)
    {
        if (!$items || !$fields) {
            return $items;
        }
        $fieldsArr = [];
        if (!is_array($fields)) {
            $arr = explode(',', $fields);
            foreach ($arr as $k => $v) {
                $fieldsArr[$v] = ['field' => $v];
            }
        } else {
            foreach ($fields as $k => $v) {
                if (is_array($v)) {
                    $v['field'] = $v['field'] ?? $k;
                } else {
                    $v = ['field' => $v];
                }
                $fieldsArr[$v['field']] = $v;
            }
        }
        foreach ($fieldsArr as $k => &$v) {
            $v = is_array($v) ? $v : ['field' => $v];
            $v['display'] = $v['display'] ?? str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
            $v['primary'] = $v['primary'] ?? '';
            $v['column'] = $v['column'] ?? 'name';
            $v['model'] = $v['model'] ?? '';
            $v['table'] = $v['table'] ?? '';
            $v['name'] = $v['name'] ?? str_replace(['_ids', '_id'], '', $v['field']);
        }
        unset($v);
        $ids = [];
        $fields = array_keys($fieldsArr);
        foreach ($items as $k => $v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
                }
            }
        }
        $result = [];
        foreach ($fieldsArr as $k => $v) {
            if ($v['model']) {
                $model = new $v['model'];
            } else {
                // 优先判断使用table的配置
                $model = $v['table'] ? \think\Db::table($v['table']) : \think\Db::name($v['name']);
            }
            $primary = $v['primary'] ?: $model->getPk();
            $result[$v['field']] = isset($ids[$v['field']]) ? $model->where($primary, 'in', $ids[$v['field']])->column($v['column'], $primary) : [];
        }

        foreach ($items as $k => &$v) {
            foreach ($fields as $m => $n) {
                if (isset($v[$n])) {
                    $curr = array_flip(explode(',', $v[$n]));

                    $linedata = array_intersect_key($result[$n], $curr);
                    $v[$fieldsArr[$n]['display']] = $fieldsArr[$n]['column'] == '*' ? $linedata : implode(',', $linedata);
                }
            }
        }
        return $items;
    }
}

if (!function_exists('var_export_short')) {

    /**
     * 使用短标签打印或返回数组结构
     * @param mixed   $data
     * @param boolean $return 是否返回数据
     * @return string
     */
    function var_export_short($data, $return = true)
    {
        return var_export($data, $return);
    }
}

if (!function_exists('letter_avatar')) {
    /**
     * 首字母头像
     * @param $text
     * @return string
     */
    function letter_avatar($text)
    {
        $total = unpack('L', hash('adler32', $text, true))[1];
        $hue = $total % 360;
        list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);

        $bg = "rgb({$r},{$g},{$b})";
        $color = "#ffffff";
        $first = mb_strtoupper(mb_substr($text, 0, 1));
        $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" dominant-baseline="central">' . $first . '</text></svg>');
        $value = 'data:image/svg+xml;base64,' . $src;
        return $value;
    }
}

if (!function_exists('hsv2rgb')) {
    function hsv2rgb($h, $s, $v)
    {
        $r = $g = $b = 0;

        $i = floor($h * 6);
        $f = $h * 6 - $i;
        $p = $v * (1 - $s);
        $q = $v * (1 - $f * $s);
        $t = $v * (1 - (1 - $f) * $s);

        switch ($i % 6) {
            case 0:
                $r = $v;
                $g = $t;
                $b = $p;
                break;
            case 1:
                $r = $q;
                $g = $v;
                $b = $p;
                break;
            case 2:
                $r = $p;
                $g = $v;
                $b = $t;
                break;
            case 3:
                $r = $p;
                $g = $q;
                $b = $v;
                break;
            case 4:
                $r = $t;
                $g = $p;
                $b = $v;
                break;
            case 5:
                $r = $v;
                $g = $p;
                $b = $q;
                break;
        }

        return [
            floor($r * 255),
            floor($g * 255),
            floor($b * 255)
        ];
    }
}

if (!function_exists('check_nav_active')) {
    /**
     * 检测会员中心导航是否高亮
     */
    function check_nav_active($url, $classname = 'active')
    {
        $auth = \app\common\library\Auth::instance();
        $requestUrl = $auth->getRequestUri();
        $url = ltrim($url, '/');
        return $requestUrl === str_replace(".", "/", $url) ? $classname : '';
    }
}

if (!function_exists('check_cors_request')) {
    /**
     * 跨域检测
     */
    function check_cors_request()
    {
        if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] && config('fastadmin.cors_request_domain')) {
            $info = parse_url($_SERVER['HTTP_ORIGIN']);
            $domainArr = explode(',', config('fastadmin.cors_request_domain'));
            $domainArr[] = request()->host(true);
            if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
                header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
            } else {
                $response = Response::create('跨域检测无效', 'html', 403);
                throw new HttpResponseException($response);
            }

            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');

            if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
                if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
                    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
                }
                if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
                    header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
                }
                $response = Response::create('', 'html');
                throw new HttpResponseException($response);
            }
        }
    }
}

if (!function_exists('xss_clean')) {
    /**
     * 清理XSS
     */
    function xss_clean($content, $is_image = false)
    {
        return \app\common\library\Security::instance()->xss_clean($content, $is_image);
    }
}

if (!function_exists('url_clean')) {
    /**
     * 清理URL
     */
    function url_clean($url)
    {
        if (!check_url_allowed($url)) {
            return '';
        }
        return xss_clean($url);
    }
}

if (!function_exists('check_ip_allowed')) {
    /**
     * 检测IP是否允许
     * @param string $ip IP地址
     */
    function check_ip_allowed($ip = null)
    {
        $ip = is_null($ip) ? request()->ip() : $ip;
        $forbiddenipArr = config('site.forbiddenip');
        $forbiddenipArr = !$forbiddenipArr ? [] : $forbiddenipArr;
        $forbiddenipArr = is_array($forbiddenipArr) ? $forbiddenipArr : array_filter(explode("\n", str_replace("\r\n", "\n", $forbiddenipArr)));
        if ($forbiddenipArr && \Symfony\Component\HttpFoundation\IpUtils::checkIp($ip, $forbiddenipArr)) {
            $response = Response::create('请求无权访问', 'html', 403);
            throw new HttpResponseException($response);
        }
    }
}

if (!function_exists('check_url_allowed')) {
    /**
     * 检测URL是否允许
     * @param string $url URL
     * @return bool
     */
    function check_url_allowed($url = '')
    {
        //允许的主机列表
        $allowedHostArr = [
            strtolower(request()->host())
        ];

        if (empty($url)) {
            return true;
        }

        //如果是站内相对链接则允许
        if (preg_match("/^[\/a-z][a-z0-9][a-z0-9\.\/]+((\?|#).*)?\$/i", $url) && substr($url, 0, 2) !== '//') {
            return true;
        }

        //如果是站外链接则需要判断HOST是否允许
        if (preg_match("/((http[s]?:\/\/)+((?>[a-z\-0-9]{2,}\.)+[a-z]{2,8}|((?>([0-9]{1,3}\.)){3}[0-9]{1,3}))(:[0-9]{1,5})?)(?:\s|\/)/i", $url)) {
            $chkHost = parse_url(strtolower($url), PHP_URL_HOST);
            if ($chkHost && in_array($chkHost, $allowedHostArr)) {
                return true;
            }
        }

        return false;
    }
}

if (!function_exists('build_suffix_image')) {
    /**
     * 生成文件后缀图片
     * @param string $suffix 后缀
     * @param null   $background
     * @return string
     */
    function build_suffix_image($suffix, $background = null)
    {
        $suffix = mb_substr(strtoupper($suffix), 0, 4);
        $total = unpack('L', hash('adler32', $suffix, true))[1];
        $hue = $total % 360;
        list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);

        $background = $background ? $background : "rgb({$r},{$g},{$b})";

        $icon = <<<EOT
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
            <path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
            <path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
            <polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
            <path style="fill:{$background};" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
            <path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
            <g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">{$suffix}</tspan></text></g>
        </svg>
EOT;
        return $icon;
    }




    // ------------自加助手函数-------------------------------

    // 生成随机字符串
    if (!function_exists('build_randstr')) {
        /**
         * 获得随机字符串
         * @param $len             需要的长度
         * @param $special        是否需要特殊符号
         * @return string       返回随机字符串
         */
        function build_randstr($len = 10, $special = false)
        {
            $chars = array(
                "0",
                "1",
                "2",
                "3",
                "4",
                "5",
                "6",
                "7",
                "8",
                "9"
            );

            if ($special) {
                $chars = array_merge($chars, array(
                    "!",
                    "@",
                    "#",
                    "$",
                    "?",
                    "|",
                    "{",
                    "/",
                    ":",
                    ";",
                    "%",
                    "^",
                    "&",
                    "*",
                    "(",
                    ")",
                    "-",
                    "_",
                    "[",
                    "]",
                    "}",
                    "<",
                    ">",
                    "~",
                    "+",
                    "=",
                    ",",
                    "."
                ));
            }

            $charsLen = count($chars) - 1;
            shuffle($chars);                            //打乱数组顺序
            $str = '';
            for ($i = 0; $i < $len; $i++) {
                $str .= $chars[mt_rand(0, $charsLen)];    //随机取出一位
            }
            return $str;
        }
    }

    //单图上传
    if (!function_exists('build_upload')) {
        /**
         * 上传单图
         */
        function build_upload($name)
        {
            // 返回结果集合
            $result = [
                'msg' => '未上传图片', // 提示信息
                'data' => null, // 返回图片路径
                'code' => 0 // 状态码
            ];

            // 通过请求对象接收图片 $_FILES[$name]
            $file = request()->file($name);

            // 如果有上传图片
            if ($file) {
                // 把图片保存指定的目录
                // DS 表示在不同系统里的斜杠
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'avatars');

                // 如果保存成功
                if ($info) {
                    // 获取上传后的图片名 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $filename = str_replace('\\', '/', $info->getSaveName());

                    // 组装数据
                    $result = [
                        'msg' => '上传成功',
                        'data' => '/uploads/avatars/' . $filename,
                        'code' => 1
                    ];
                } else {
                    $result['msg'] = $file->getError();
                }
            }

            return $result;
        }
    }

    //多图上传
    if (!function_exists('build_uploads')) {
        // 多图上传
        if (!function_exists('build_uploads')) {
            function build_uploads($name)
            {
                // 返回结果集合
                $result = [
                    'msg' => '未上传图片',
                    'data' => '',
                    'code' => 0,
                ];

                // 通过请求对象接收图片（多个）
                $files = request()->file($name);

                // 如果有上传图片
                if ($files) {
                    $uploadedFiles = []; // 记录已上传的文件路径
                    $uploadFailed = false; // 标记是否有上传失败的文件
                    $errorMessage = '';
                    $publicPath = ROOT_PATH . 'public' . DS; // 公共路径前缀

                    // 遍历所有上传的文件
                    foreach ($files as $file) {
                        // 处理单个文件上传
                        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'posts');

                        // 如果保存成功
                        if ($info) {
                            // 获取上传后的完整路径（包含子目录）
                            $fullPath = $info->getPathname();
                            $uploadedFiles[] = $fullPath;
                        } else {
                            // 标记上传失败
                            $uploadFailed = true;
                            $errorMessage = $file->getError();
                            break; // 一旦有文件上传失败，立即停止处理
                        }
                    }

                    // 如果有上传失败的文件，删除所有已上传的文件
                    if ($uploadFailed) {
                        // 删除已上传的文件
                        foreach ($uploadedFiles as $filePath) {
                            if (file_exists($filePath)) {
                                unlink($filePath);
                            }
                        }

                        $result = [
                            'msg' => '上传失败：' . $errorMessage,
                            'data' => '',
                            'code' => 0,
                        ];
                    } else {
                        // 所有文件上传成功，组装结果
                        $successPaths = array_map(function ($filePath) use ($publicPath) {
                            // 移除公共路径前缀，保留相对路径
                            $relativePath = str_replace($publicPath, '', $filePath);
                            // 替换反斜杠为正斜杠（Windows兼容性）
                            $relativePath = str_replace('\\', '/', $relativePath);
                            return '/' . $relativePath;
                        }, $uploadedFiles);

                        $result = [
                            'msg' => '上传成功',
                            'data' => implode(',', $successPaths),
                            'code' => 1,
                        ];
                    }
                }

                return $result;
            }
        }
    }

    //脱敏函数
    if (!function_exists('build_encrypt')) {
        /**
         * 数据脱敏函数
         * @param $string 需要脱敏值
         * @param int $start 开始
         * @param int $length 结束
         * @param string $re 脱敏替代符号
         * @return bool|string
         * 例子:
         * build_encrypt('13126989876', 3, 4); //131****9876
         * build_encrypt('张三四', 0, -1); //**四
         */
        function build_encrypt($string, $start = 3, $length = 4, $re = '*')
        {
            // 备份原值
            $back = $string;

            if (empty($string)) {
                return false;
            }

            // 存放脱敏值数组
            $StrArr = [];

            // 获取字符串的长度
            $mb_strlen = mb_strlen($string);

            // 循环把 字符串 变为数组
            while ($mb_strlen) {
                // 返回截取部分字符
                $StrArr[] = mb_substr($string, 0, 1, 'utf8');
                // 每次循环字符串的第一位字符不要并且重新赋值
                $string = mb_substr($string, 1, $mb_strlen, 'utf8');
                // 重新获取字符串长度
                $mb_strlen = mb_strlen($string);
            }

            // 获取数组长度
            $strlen = count($StrArr);

            $begin = $start >= 0 ? $start : ($strlen - abs($start));
            $end = $last = $strlen - 1;

            if ($length > 0) {
                $end = $begin + $length - 1;
            } elseif ($length < 0) {
                $end -= abs($length);
            }

            for ($i = $begin; $i <= $end; $i++) {
                $StrArr[$i] = $re;
            }

            if ($begin >= $end || $begin >= $last || $end > $last) return $back;
            return implode('', $StrArr);
        }
    }

    // 后端请求封装
    if (!function_exists('HttpRequest')) {
        // 后端中发送请求
        function HttpRequest($url, $data = null)
        {
            if (function_exists('curl_init')) {
                // 初始化
                $curl = curl_init();

                // 设置请求地址
                curl_setopt($curl, CURLOPT_URL, $url);

                // 设置http某些配置
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 验证ssl证书
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);

                // 判断传进来的参数是否不为空
                if (!empty($data)) {
                    // 设置该请求为POST
                    curl_setopt($curl, CURLOPT_POST, 1);
                    // 把参数带入请求
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                }

                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $output = curl_exec($curl);
                curl_close($curl);
                return $output;
            } else {
                return false;
            }
        }
    }

    // 创建订单
    if (!function_exists('build_code')) {
   /**
    * 生成唯一订单号
    * @param String $prefix 指定的订单前缀
    * @return String  返回字符串
    */
   function build_code($prefix = "")
   {
      // 设置时区
      @date_default_timezone_set('PRC');

      // 以时间+随机数组成一个订单主体 2024111217232312345
      $order_id_main = date('YmdHis') . rand(10000, 99999);

      // 订单号码主体长度
      $order_id_len = strlen($order_id_main);

      // 拼接循环后结果
      $order_id_sum = 0;

      for ($i = 0; $i < $order_id_len; $i++) {
         $order_id_sum += (int)(substr($order_id_main, $i, 1));
      }
      // 唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
      $osn = $prefix . $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT); //生成唯一订单号
      return $osn;
   }
}
}
